package com.why.cachecloud.service.inspect.impl;

import com.why.cachecloud.service.alert.impl.BaseAlertService;
import com.why.cachecloud.configuration.constant.AppStatusEnum;
import com.why.cachecloud.configuration.constant.InstanceStatusEnum;
import com.why.cachecloud.dao.InstanceDao;
import com.why.cachecloud.entity.AppDesc;
import com.why.cachecloud.entity.InstanceAlertValueResult;
import com.why.cachecloud.entity.InstanceInfo;
import com.why.cachecloud.util.ConstUtils;
import com.why.cachecloud.web.service.AppService;
import com.why.cachecloud.web.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class InstanceStateInspector extends BaseAlertService {

    /**
     * 实例相关
     */
    @Autowired
    private InstanceDao instanceDao;
    @Autowired
    private AppService appService;
    @Autowired
    private UserService userService;

    public boolean inspect() {

        List<InstanceAlertValueResult> alertInstInfo = new ArrayList<>();
        List<InstanceInfo> heartStopInstances = instanceDao.getAllHeartStopInstance();
        if (!CollectionUtils.isEmpty(heartStopInstances)) {
            for (InstanceInfo info : heartStopInstances) {
                long appId = info.getAppId();
                AppDesc appDesc = appService.getByAppId(appId);
                if (appDesc.getStatus() != AppStatusEnum.STATUS_PUBLISHED.getStatus()) {
                    continue;
                }
                appDesc.setOfficer(userService.getOfficerName(appDesc.getOfficer()));
                InstanceAlertValueResult instanceAlert = new InstanceAlertValueResult();
                instanceAlert.setInstanceInfo(info);
                instanceAlert.setAppId(appId);
                instanceAlert.setAppDesc(appDesc);
                instanceAlert.setOtherInfo(InstanceStatusEnum.getByStatus(info.getStatus()).getInfo());
                alertInstInfo.add(instanceAlert);
            }
            if (alertInstInfo.isEmpty()) {
                return true;
            }
            String emailTitle = "#### Redis实例异常状态监控报警 ";

            StringBuilder sb = new StringBuilder();
            sb.append(emailTitle).append(ConstUtils.getPhoneString()).append(" \n\n");
            for (InstanceAlertValueResult item : alertInstInfo) {
                sb.append("--- \n\n");
                sb.append("应用id：").append(item.getAppId()).append(" \n\n");
                sb.append("应用名：").append(item.getAppDesc().getName()).append(" \n\n");
                sb.append("负责人：").append(item.getAppDesc().getOfficer()).append(" \n\n");
                sb.append("ip：").append(item.getInstanceInfo().getIp()).append(" \n\n");
                sb.append("port：").append(item.getInstanceInfo().getPort()).append(" \n\n");
                sb.append("状态：").append(item.getInstanceInfo().getStatusDesc()).append(" \n\n");
                sb.append("说明：").append(item.getOtherInfo()).append(" \n\n");
            }

            dingTalkComponent.sendDingTalkToAdmin(sb.toString());
            logger.info(sb.toString());
        }
        return true;
    }

}
